// pages/home/home.js
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '../../store/store'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //存放轮播图
    swiperList: [],
    //存放九宫格
    getGrdList: [],
    //父子通信
    count: 0
  },
  //promsie化案例
  async getInfo() {
    const res = await wx.p.request({
      method: 'GET',
      url: 'https://www.escook.cn/api/get',
      data: {
        name: 'zs',
        age: 20
      }
    })
    // console.log(res);
  },
  //ref案例
  getChild() {
    const child = this.selectComponent('#cA')
    // child.setData({
    //   count:child.properties.count +1
    // })
    child.addCount()
  },
  //组件通信接收子组件信息函数
  syncCount(e) {
    this.setData({
      count: e.detail.value
    })
  },
  getSwiperList() {
    wx.request({
      url: 'https://www.escook.cn/slides',
      method: 'GET',
      success: (res) => {
        // console.log(res);
        this.setData({
          swiperList: res.data
        })
      }
    })
  },
  getGrdList() {
    wx.request({
      url: 'https://www.escook.cn/categories',
      method: 'GET',
      success: (res) => {
        // console.log(res);
        this.setData({
          getGrdList: res.data
        })
      }
    })
  },
  //store案例
  btnHeader1(e) {
    this.updateNum1(e.target.dataset.step)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSwiperList()
    this.getGrdList()
    this.getInfo()
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['numA','numB','sum'],
      actions: ['updateNum1']
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.storeBindings.detroyStoreBindings()
  },
})